package com.gomyb.admin.module.product.service.impl;

import com.gomyb.admin.module.product.model.AppProduct;
import com.gomyb.admin.module.product.service.AppProductService;
import com.gomyb.common.base.BaseDao;
import com.gomyb.common.base.BaseServiceImpl;
import com.gomyb.common.page.PageParam;
import com.gomyb.common.page.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 */
@Service
public class AppProductServiceImpl extends BaseServiceImpl<AppProduct, Long> implements AppProductService {

    @Override
    @Autowired
    public void setDao(BaseDao<AppProduct, Long> dao) {
        super.dao = dao;
    }

    @Override
    public PageResult<AppProduct> findPageBySql(PageParam pageParam) {
        //language=SQL
        StringBuilder hql = new StringBuilder("select\n" +
                "  new\n" +
                "  com.gomyb.admin.module.product.model.AppProduct(\n" +
                "      t1.id, t1.name, t1.model, t1.eachCount, t1.wholesaleIn, t1.wholesaleOut, t1.retailPrice, t1.categoryId,\n" +
                "      t2.name\n" +
                "    )\n" +
                "FROM AppProduct t1,\n" +
                "     AppProductCategory t2\n" +
                "where t1.categoryId = t2.id");
        Map<String, String> data = pageParam.getData();
        if (StringUtils.isNotEmpty(data.get("name"))) {
            hql.append(" and t1.name like '%").append(data.get("name")).append("%' ");
        }
        if (StringUtils.isNotEmpty(data.get("categoryId"))) {
            hql.append(" and t1.categoryId = ").append(data.get("categoryId"));
        }
        return super.findPageByHql(hql.toString(), pageParam);
    }
}
